package performance;

import org.tinygroup.logger.Logger;
import org.tinygroup.logger.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;


public class BaseTpsTest {
    protected static Logger logger = LoggerFactory.getLogger("TpsTest");
	protected static final String JDBC_PROPERTIES = "jdbc.properties";
	protected static String driver = "";
	protected static String user = "";
	protected static String password = "";

	protected static String url = "";

	static {
		init();
	}

	private static void init(){
		Properties prop = new Properties();
		InputStream in = ResourceLoadUtil.getLocationInputStream(JDBC_PROPERTIES);

		try {
			prop.load(in);
			url = prop.getProperty("url").trim();
			password = prop.getProperty("password").trim();
			user=prop.getProperty("user").trim();
			driver=prop.getProperty("driver").trim();
		} catch (IOException e) {
			logger.errorMessage(e.getMessage(),e);
		}
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	protected void close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	protected void close(Statement stmt) {
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	protected Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, user, password);
	}

	protected void close(PreparedStatement pStmt) {
		if (pStmt != null) {
			try {
				pStmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	protected void close(ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public void tearDown() {

	}
	
}
